package com.cow.sram.module.archives;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cow.framework.core.base.BaseComponent;
import com.cow.sram.data.dao.OrginfoDAO;
import com.cow.sram.data.entity.Orginfo;
import com.cow.sram.data.entity.Resourceinfo;
import com.cow.sram.data.mapper.ResourceinfoMapper;

/**
 * 组织管理
 * @author Administrator
 *
 */
@Service
public class OrgInfoService extends BaseComponent{
	@Autowired
	OrginfoDAO orginfoDao;
	@Autowired
	ResourceinfoMapper resourceinfoMapper;
	
	/**查询全部*/
	public List<Orginfo> findOrginfo(){
		return orginfoDao.findAll();
	}
	/**查询组织*/
	public List<Resourceinfo> queryUserOrg(){
		Map parmMap=new HashMap();
		parmMap.put("userid", getCurUserId());
		return resourceinfoMapper.queryUserOrg(parmMap);
	}
	
	/**根据ID查询*/
	public Orginfo getOrgById(Integer id){
		return orginfoDao.findOne(id);
	}
	
	/**新增一条记录*/
	public Orginfo saveOrginfo(Orginfo org){
		return orginfoDao.save(org);
	}
	
	/**
	 *	获取组织编码 
	 */
	public String getMaxOrgCode(Integer superorgid){
		String orgCode=orginfoDao.getMaxOrgCode(superorgid);
		if(!orgCode.equals("0")){
			int order = Integer.valueOf(orgCode.substring(orgCode.length() - 2)) + 1;
			orgCode = orgCode.substring(0,orgCode.length() - 2) + String.format("%02d", order);
		}
		if(orgCode.equals("0")){//第一个节点
			//获取上级组织信息
			Orginfo fatherOrg = getOrgById(superorgid);
			orgCode = fatherOrg.getOrgcode() + "01";
		}
		return orgCode;
	}

	
	/**删除记录*/
	public void delOrginfo(Integer id){
		orginfoDao.delete(id);
	}

}
